﻿using Company.Core.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace Company.Core.Models
{
    /// <summary>
    /// 非托管二维数组（以数组的形式描述内存中的图像，考虑适用于2维图像、3维图像）T为 ColorBGR/ColorBGRA
    /// </summary>
    /// <typeparam name="T"> ColorBGR/ColorBGRA</typeparam>
    public class UnmanagedArray2D<T> : UnmanagedArrayBase where T : struct
    {
        public int Width { get; set; }
        public int Height { get; set; }

        /// <summary>
        /// 图像 Stride（步长/跨距）有时称为Pitch，指的是图像中一行像素在内存中所占的字节数。
        /// Stride = 图像的实际宽度（像素数） × 每个像素的字节数
        /// </summary>
        public int Stride => Width * Marshal.SizeOf(typeof(T));

        public UnmanagedArray2D(int width, int height, bool isResetMemory = true) : base(width * height, Marshal.SizeOf(typeof(T)), isResetMemory)
        {
            Width = width;
            Height = height;
        }

        public UnmanagedArray2D<T> DeepCopy()
        {
            var temp = new UnmanagedArray2D<T>(Width, Height);
            MemoryHelper.CopyMemory(temp.IntPtr, this.IntPtr, Size);
            return temp;
        }
    }
}
